<?php
class Site extends Model_Abstract
{
	public function getDomainById($site_id)
	{
			
	}
		
	public function getSiteByDomain($domain)
	{
		try{
			$sql = 'select * from sites where domain = ? limit 1';
			$result = $this->_connection->query($sql, array($domain));
			if(count($result) == 1){
				return $result[0];
			}else{
				return false;
			}
		}
		catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}

	public function getWinners($siteid)
	{
		try{
			$sql = 'select * from winners where site_id = ?';
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result;
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getTodayLeaders($siteid){
		try{
			$sql = " SELECT locale FROM sites WHERE id=? LIMIT 1 ";
			$result = $this->_connection->query($sql, array($siteid));
			$locationStr = ' concat(city, ", " , state) ';
			if(count($result) >= 1){
				$locationStr = strtolower($result[0]['locale']) == 'uk' ? 'city' : $locationStr;
			}
			$sql = "select concat(firstname, ' ', upper(left(lastname,1))) as username, daily
					, " . $locationStr . " as location from users where site_id = ? and daily>0 order by daily desc limit 0, 20";
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result;
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getWeeklyLeaders($siteid){
		try{
			$sql = " SELECT locale FROM sites WHERE id=? LIMIT 1 ";
			$result = $this->_connection->query($sql, array($siteid));
			$locationStr = ' concat(city, ", " , state) ';
			if(count($result) >= 1){
				$locationStr = strtolower($result[0]['locale']) == 'uk' ? 'city' : $locationStr;
			}
			$sql = "select concat(concat(firstname, ' '),upper(left(lastname,1))) as username, weekly
					, " . $locationStr . " as location from users where site_id = ?  and weekly > 0 order by weekly desc limit 0, 20";
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result;
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getMonthlyLeaders($siteid){
		try{
			$sql = " SELECT locale FROM sites WHERE id=? LIMIT 1 ";
			$result = $this->_connection->query($sql, array($siteid));
			$locationStr = ' concat(city, ", " , state) ';
			if(count($result) >= 1){
				$locationStr = strtolower($result[0]['locale']) == 'uk' ? 'city' : $locationStr;
			}
			$sql = "select concat(concat(firstname, ' '),upper(left(lastname,1))) as username, monthly
					, " . $locationStr . " as location from users where site_id = ? and monthly > 0 order by monthly desc limit 0, 20";
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result;
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAllLeaders($siteid){
		try{
			$sql = " SELECT locale FROM sites WHERE id=? LIMIT 1 ";
			$result = $this->_connection->query($sql, array($siteid));
			$locationStr = ' concat(city, ", " , state) ';
			if(count($result) >= 1){
				$locationStr = strtolower($result[0]['locale']) == 'uk' ? 'city' : $locationStr;
			}
			$sql = "select concat(concat(firstname, ' '),upper(left(lastname,1))) as username, won
					, " . $locationStr . " as location from users where site_id = ? and won>0 order by won desc limit 0, 20";
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result;
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getMonetizationIds($siteid){
		try{
			$sql = 'select monetization1, monetization2, monetization3, monetization4 from sites where id = ?';
			$result = $this->_connection->query($sql, array($siteid));
			if(count($result) >= 1){
				return $result[0];
			}
			else{
				return false;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function wonJackpot($id)
	{
		try{
			$sql = 'update sites set jackpot=0 where id=?';
			$this->_connection->execute($sql, array($id));
		}
		catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function updateJackpot($id, $num)
	{
		try
		{
			$sql = 'update sites set jackpot=jackpot+0.01*? where id=?';
			$this->_connection->execute($sql, array($num, $id));
		}
		catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}